<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/25
 * Time: 16:24
 */
namespace app\open_card\controller;

use app\common\helper\setting_helper;
use app\common\model\OpenCard;
use app\common\model\Setting;
use think\facade\Filesystem;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class Admin extends AdminBase
{
    public function index()
    {
        $page_no = Request::param('page_no', 1);

        $filter['company_id'] = $this->company_id;
        $filter['status']     = 1;

        $card_list = array();

        $card_list = OpenCard::where($filter)->select()->toArray();
        foreach ($card_list as $k => $v) {
            $card_list[$k]['card_link'] = url('/admin/index/index', ['card_id'=>$v['id']]);
        }

        return View::fetch('card_list', [
            'card_list' => $card_list
        ]);
    }

    public function add()
    {
//        $company_remain = Company::get_company_level_remain($this->company_id);
//        if ($company_remain <= 0) {
//            return '公众帐号数量已达上限';
//        }

        return View::fetch('card_add');
    }
    public function edit()
    {
        $id = Request::param('id', 0);
        $info = _uri('open_card', ['id' => $id]);
        if (!$info) {
            return _return('该公众号不存在或已删除');
        }

        return View::fetch('card_add', [
            'info' => $info
        ]);
    }


    public function save()
    {
        $id        = Request::param('id', 0);
        $title     = trim(strip_tags(Request::param('title')));
        //$card      = trim(strip_tags(Request::getParam('card')));
        $wechat_id = trim(strip_tags(Request::param('wechat_id')));
        $domain    = trim(strip_tags(Request::param('domain')));
        $intro     = trim(strip_tags(Request::param('intro')));

        if (!$wechat_id) {
            return _return('微信号不能为空');
        }

        $old_info = _uri('open_card', array('domain'=>$domain, 'status'=>1));
        if ($old_info && $id != $old_info['id']) {
            return _return('二级域名已经存在');
        }

        $info = array(
            'company_id'  => $this->company_id,
            'user_id'     => $this->user_id,
            'title'       => $title,
            'wechat_id'   => $wechat_id,
            //'card'        => $card,
            'intro'       => $intro,
            'status'      => 1
        );
        if (!$id) {
            $info['domain'] = $domain;
        }

        if (isset($_FILES['cover']) && $_FILES['cover']['name']) {
            if (!$id && !$_FILES['cover']['name']) {
                return _return('请选择封面图片');
            }

            $fail_msg = check_upload($_FILES['cover']);
            if ($fail_msg) {
                return _return($fail_msg);
            }

            $file = request()->file('cover');

            $file_path = Filesystem::disk('public')->putFile(config('admin.upload_dir'), $file);

            if (isset($file_path) && !empty($file_path)) {
                $info['cover'] = $file_path;
            }

            make_thumb($file_path, 'open_card');
        }

        if ($id) {
            OpenCard::update($info,['id' => $id]);
            $notice = '编辑';
        } else {
            OpenCard::create($info);
            //open_card_helper::init_open_card($open_card_id, $this->company_id);
            $notice = '添加';

//            if (!ONDEV) {
//                // 添加新公众号的监听
//                $mail_title   = user_helper::display_name($this->user_id).'添加了新公众号：'.$info['title'];
//                $mail_content = $mail_title;
//                $user_info = get_res_info('user', $this->user_id);
//                if (isset($user_info['email'])) {
//                    $mail_content .= '<br><br>他的邮箱是：'.$user_info['email'];
//                }
//                $mail_content .= '<br><br>'.date('Y年m月d日 H时i分s秒');
//                send_email(MONITOR_EMAIL, $mail_title, $mail_content);
//            }
        }

        return redirect(url('/open_card/admin/index'));
    }

    public function check_title_info($title = '', $id = 0)
    {
        if (!$title) {
            return ['info'=>'ok'];
        }

        $old_info = _uri('open_card', array('title'=>$title, 'status'=>1));
        if ($old_info && $id != $old_info['id']) {
            return ['info'=>'公共号名称已存在'];
        }

        return ['info'=>'ok'];
    }

    public function check_domain_info($domain = '', $id = 0)
    {
        if (!$domain) {
            return 'ok';
        }

//        $default_domain = open_card_config::$default_domain;
//        if (in_array($domain, $default_domain)) {
//            return '二级域名已经存在';
//        }

        $old_info = _uri('open_card', array('domain'=>$domain, 'status'=>1));
        if ($old_info && $id != $old_info['id']) {
            return '二级域名已经存在';
        }

        return 'ok';
    }

    /**
     * ajax 验证card domain
     * @return string
     */
    public function check_info()
    {
        $title     = trim(Request::param('title'));
        //$wechat_id = trim(Request::getParam('wechat_id'));
        //$card      = trim(Request::getParam('card'));
        $domain    = trim(Request::param('domain'));
        $id        = Request::param('id', 0);

        $check_title  = $this->check_title_info($title, $id);
        if ($check_title != 'ok') {
            return $check_title;
        }
        /*
        $check_wechat_id   = $this->check_wechat_id_info($wechat_id, $id);
        if ($check_wechat_id != 'ok') {
            return $check_wechat_id;
        }
        $check_card   = $this->check_card_info($card, $id);
        if ($check_card != 'ok') {
            return $check_card;
        }
        */
        $check_domain = $this->check_domain_info($domain, $id);
        if ($check_domain != 'ok') {
            return  $check_domain;
        }

        return  'ok';
    }
}